package org.yanghaokun.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yanghaokun.basic.util.AjaxResult;
import org.yanghaokun.basic.util.PageList;
import org.yanghaokun.org.domain.DictionaryDetail;
import org.yanghaokun.org.query.DictionaryDetailQuery;
import org.yanghaokun.org.service.IDictionaryDetailService;

import java.util.List;

@RestController
@RequestMapping("detail")
@Api(value = "数据字典接口",description = "数据字典接口详细描述")
//@GetMapping =  @RequestMapping(value = "xxxx",method = RequestMethod.GET)
//@PostMapping = @RequestMapping(value = "xxxx",method = RequestMethod.POST)
//@DeleteMapping
//@PutMapping
//@PatchMapping
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService service;

    //增加和修改的实现
    @PutMapping
//    AjaxResult是为了封装处理的成功或则失败的信息返回给前台
    @ApiOperation(value = "数据产品信息添加或修改",notes = "如果有id就是修改否则就是添加！")
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail detail){//需要将数据添加所以需要将数据传到请求体中，用@RequestBody
        //添加和修改就是看是否由id
        try {
            if (detail.getId() == null) {
                service.add(detail);
            }else {
                service.update(detail);
            }
            //成功直接返回成功信息
            return  AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败啦呢！来核妲己玩耍吧" + e.getMessage());
        }
    }

    //删除   delete
    //写一个接口必须思考三个方面
    // 1 访问地址
    // 2 传递什么参数
    // 3 返回什么值 yes no JsonResult(yes/no msg)
    // 其实对于增删改，我们只关心是否成功，已经失败了的信息
    // 方案1： 字符串{success:true,message:"操作失败“}
    // 方案2： 写一个对象返回+@ResponseBody----》{success:true,message:"操作失败“}
    @DeleteMapping("/{id}")//这是删除的注解
    @ApiOperation(value = "字典细节删除",notes = "删除")
    public AjaxResult del(@PathVariable("id") Long id){//获取路径里面的id好删除对应的信息就用注解@PathVariable
        try {
            service.del(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败" + e.getMessage());
        }
    }

    //获取一个
    @GetMapping("/{id}")
    @ApiOperation(value = "字典细节查询",notes = "查询一条")
    public DictionaryDetail getOne(@PathVariable("id") Long id){
        return service.findOne(id);
    }

    //获取多个
    @GetMapping
    @ApiOperation(value = "字典细节查询",notes = "查询全部")
    public List<DictionaryDetail> getAll(){
        return service.findAll();
    }

    //axios get不支持传对象
    @PostMapping
    public PageList<DictionaryDetail> list(@RequestBody DictionaryDetailQuery query){

        return service.getList(query);
    }

}
